﻿@using ST = EmoneyWeiXinCommon.StringTools;
@using EmoneyWeiXinEntity;
@{
    //Layout = "~/Views/Shared/_Layout.cshtml";
    //ViewBag.Title = "二维码生成";
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "二维码生成";
}

<div class="right">
    <h3>二维码生成</h3>
    <input style="display: none" id="hidid" type="text">
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_top_set">
                <div class="manage_top_set_left">
                    <ul>
                        <li>
                            <p id="pType">
                            </p>
                            <p>
                                <span>参数值：</span><input id="txtValue" type="text">
                            </p>

                        </li>

                        <li>
                            <p class="manage_register_p"><span>名称：</span><input id="txtName" type="text"></p>
                        </li>
                        <li>
                            <p>
                                <span>状态：</span>
                                <select id="drpStatus">
                                    <option value="0">全部</option>
                                    <option value="2">未启用</option>
                                    <option value="1">启用</option>
                                    @* <option value="3">无效</option>*@
                                </select>
                            </p>
                            <p><span>客服号：</span><input id="txtKF_ID" type="text"></p>
                        </li>
                        <li class="manage_top_set_left_li">
                            <p class="manage_register_p">
                                <span>备注：</span>
                                <textarea id="txtMemo"></textarea>
                            </p>
                        </li>
                    </ul>
                </div>
                <div class="manage_top_set_right manage_top_set_right02">
                    <p>
                        <a>
                            <img id="imgTwoCode" src=""></a>
                    </p>
                </div>
            </div>
            <p class="manage_add">
                <a id="btnQuery" class="manage_add_font">查询</a>
                <a id="btnCreate">生成预览</a>
                <a id="btnSave" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3,ViewBag.leftid))'>保存</a>
                <a id="btnCancel" class="manage_add_font">取消</a>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>编号</span></th>
                    <th><span>分类</span></th>
                    <th><span>名称</span></th>
                    <th><span>参数值</span></th>
                    <th><span>状态</span></th>
                    <th><span>客服号</span></th>
                    <th><span>备注</span></th>
                    <th><span>预览</span></th>
                    <th><span>添加者</span></th>
                    <th><span>添加时间</span></th>
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>

            <div class="manage_page" id="manage_page">
            </div>
            <!--manage_bottom end-->
        </div>
        <!--manage end-->
    </div>
</div>
<div style="width: 100%; height: 100%; display: none; position: absolute; top: 0px; left: 0px;" id="loadDiv">
    <div id="subLoadDiv">
        <img src="@Url.Content("~/Static/Image/load.gif")" />
    </div>
</div>
<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        createMemu();
        //查询
        $("#btnQuery").click(function () {
            pageIndex = 1;
            gotoPage();
        });
        //生产浏览
        $("#btnCreate").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var value = $("#txtValue").val();
                create(id, value);
            }
        });

        //保存
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var typeid = $("#drpType").val();
                var name = $("#txtName").val();
                var value = $("#txtValue").val();
                var status = $("#drpStatus").val();
                var memo = $("#txtMemo").val();
                var kfid = $("#txtKF_ID").val();
                var imgurl = $("#imgTwoCode").attr("src");
                if (id > 0)
                    operate("2", id, typeid, name, value, status, memo, kfid, imgurl)
                else
                    operate("1", "0", typeid, name, value, status, memo, kfid, imgurl)
            }
        });
        //取消
        $("#btnCancel").click(function () {
            reseting();
        });
    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            bindtype(function (result) {
                if (result == "1") {
                    gotoPage();

                }
            });
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
           
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    reseting();
                    getContentMemu();
                })
                getContentMemu();
            }
        });
    }
    var datatype = "";
    function bindtype(fn) {
        $.ajax({
            url: "@ST.GetTrendsFile("TwoCode/GetTwoCodeType")",
            type: "POST",
            async: true,
            data: { weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#pType").html("");
                    var d = datatype;
                    var html = '<span>分类：</span><select id="drpType" ><option value="0">全部</option>';
                    for (var i = 0; i < d.length; i++) {
                        html += ' <option value="' + d[i]["ID"] + '">' + d[i]["name"] + '</option>';
                    }
                    html += '</select><b>(<span id="labBeginVal"></span>-<span id="labEndVal"></span>)</b>';
                    $("#pType").append(html);
                    showlabelval();
                    fn('1');
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    function showlabelval() {
        $("#drpType").change(function () {
            var typeid = $(this).val();
            showlabelvalByTypeID(typeid);
        })
    }
    function showlabelvalByTypeID(typeid) {
        if (datatype != "") {
            for (var i = 0; i < datatype.length; i++) {
                if (datatype[i]["ID"] == typeid) {
                    var bval = datatype[i]["begin_val"];
                    var eval = datatype[i]["end_val"];
                    $("#labBeginVal").text(bval);
                    $("#labEndVal").text(eval);
                }
            }
        }
    }

    function create(id, value) {
        $.ajax({
            url: "@ST.GetTrendsFile("TwoCode/CreateTwoCode")",
            type: "POST",
            async: true,
            data: { weixinid: weixinid, id: id, value: value },
            beforeSend: function () {
                showLoading();
            },
            complete: function () {
                hideLoading();
            },
            success: function (data) {
                var arr = data.split(',');
                if (arr[0] == "1") {
                    $("#imgTwoCode").attr("src", arr[1]);
                    alert("操作成功!");
                }
                else {
                    alert(arr[1]);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function ischeck() {
        var drpType = $("#drpType").val();
        if (drpType == "") {
            alert("分类不能空！");
            $("#drpType").focus();
            return false;
        }
        if (drpType == "0") {
            alert("请选择分类！");
            $("#drpType").focus();
            return false;
        }
        var txtName = $("#txtName").val();
        if (txtName == "") {
            alert("名称不能空，请重新输入！");
            $("#txtName").focus();
            return false;
        }
        var txtValue = $("#txtValue").val();
        if (txtValue == "")
        { alert("参数值不能空，请重新输入！"); $("#txtValue").focus(); return false; }
        var value = 0;
        try {
            value = parseInt(txtValue);
        }
        catch (err) {
            alert("参数值格式不正确，请重新输入！"); $("#txtValue").focus(); return false;
        }
        var beginval = parseInt($("#labBeginVal").text());
        var endval = parseInt($("#labEndVal").text());

        if (!(value >= beginval && value <= endval))
        { alert("参数值不能超过此分类的参数区间，请重新输入！"); $("#txtValue").focus(); return false; }

        var drpStatus = $("#drpStatus").val();
        if (drpStatus == "0") {
            alert("请选择状态！");
            $("#drpStatus").focus();
            return false;
        }

        var memo = $("#txtMemo").val();
        if (memo.length > 100)
        { alert("备注长度不能超过100，请重新输入！"); $("#txtMemo").focus(); return false; }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        $("#dvList").html("");
        ajaxdata["index"] = pageIndex;
        ajaxdata["size"] = pageSize;
        ajaxdata["typeid"] = $("#drpType").val();
        ajaxdata["name"] = $("#txtName").val();
        ajaxdata["value"] = $("#txtValue").val();
        ajaxdata["status"] = $("#drpStatus").val();
        ajaxdata["memo"] = $("#txtMemo").val();
        ajaxdata["weixinid"] = weixinid;
        $.ajax({
            url: "@ST.GetTrendsFile("TwoCode/getTwoCodeList")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            success: function (result) {
                if (result != "" && result != null && result != undefined) {
                    var arrData = result.split("~");
                    totalCount = arrData[0];
                    var data = arrData[1];
                    var d = eval(data);
                    var html = "";
                    for (var i = 0; i < d.length; i++) {
                        html += "<tr>";
                        html += getTxtTd(d[i]["ID"]) + getTxtTd(d[i]["typename"]) + getTxtTd(d[i]["name"])
                        html += getTxtTd(d[i]["value"]) + getTxtTd(d[i]["statusname"]) + getTxtTd(d[i]["kf_id"])
                        html += getTxtTd(d[i]["memo"]) + getImgTxtTd(d[i]["imgurl"]) + getTxtTd(d[i]["username"]) + getTxtTd(d[i]["updatetime"].replace(/T/g, " "))
                        html += "<td><a class=\"manage_bottom_font blue\" onclick=\"selected('" + d[i]["ID"] + "','" + d[i]["typeid"] + "','" + d[i]["name"] + "','" + d[i]["value"] + "','" + d[i]["status"] + "','" + d[i]["memo"] + "','" + d[i]["kf_id"] + "','" + d[i]["imgurl"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(3,ViewBag.leftid))'>编辑</a>";
                        html += "<a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["ID"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(4,ViewBag.leftid))'>删除</a></td>";
                        html += '</tr>';
                    }
                    $("#dvList").html(html);
                    PageCountHtml();
                } else {
                    $("#dvList").html("没有查询到相关数据!");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }
    function getImgTxtTd(s) {
        return '<td><span class="preview"><img width="60" height="60" src="' + s + '"></span></td>';
    }
    //编辑，删除，关闭操作
    function operate(op, id, typeid, name, value, status, memo, kfid, imgurl) {
        $.ajax({
            url: "@ST.GetTrendsFile("TwoCode/UpdateTwoCodeInfo")",
            type: "POST",
            async: true,
            data: { flag: op, id: id, typeid: typeid, name: name, value: value, status: status, memo: memo, kfid: kfid, imgurl: imgurl, weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    reseting();
                    gotoPage();
                    alert("操作成功。");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#drpType").val("0");
        $("#txtName").val("");
        $("#txtValue").val("");
        $("#drpStatus").val("0");
        $("#txtMemo").val("");
        $("#txtKF_ID").val("");
        $("#imgTwoCode").attr("src", "");
        $("#labBeginVal").text("");
        $("#labEndVal").text("");

    }
    //选择行
    function selected(id, typeid, name, value, status, memo, kfid, imgurl) {
        $("#hidid").val(id);
        $("#drpType").val(typeid);
        $("#txtName").val(name);
        $("#txtValue").val(value);
        $("#drpStatus").val(status);
        $("#txtMemo").val(memo);
        $("#txtKF_ID").val(kfid);
        $("#imgTwoCode").attr("src", imgurl);
        showlabelvalByTypeID(typeid);
    }
    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }


    function showLoading() {
        var margintop = (document.documentElement.clientHeight) / 2 - 20;
        var marginleft = (document.documentElement.clientWidth) / 2 - 20;
        $("#subLoadDiv").css({ "margin-top": margintop, "margin-left": marginleft });
        $("#loadDiv").show();
    }

    function hideLoading() {
        $("#loadDiv").hide();
    }

</script>
